const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const getUserProfile = function (callback) {

    wx.getUserProfile({
        desc: "用于考生信息",
        success: res => {
            // wx.su.toast(res)
            let userinfo = getApp().userinfo;
            wx.su.sqlog(res);

            userinfo.nick_name = res.userInfo.nickName;
            userinfo.portrait = res.userInfo.avatarUrl;

            userinfo.wx_user_info = JSON.stringify(res.userInfo)

            getApp().writeUserInfo(userinfo)
            callback();
            return false;
            wx.su.request({
                url: wx.surls.user.updateuserinfo(),
                method: 'POST',
                data: postData,
                withoutOau: true,
                dataType: "json",
                success(res) {
                    if (res.statusCode == 200) {
                        getApp().writeUserInfo(res.data)

                    } else {
                        wx.su.toast("数据更新失败，请检查网络后重试");
                    }
                },
                fail: function () {
                    wx.su.toast("数据更新失败，请检查网络后重试");

                }
            })
        },
        fail: err => {
            wx.showModal({
                title: '匿名使用',
                content: '选择匿名使用，系统部分功能将无法使用，确定匿名使用吗?',
                showCancel: true,
                cancelText: '匿名使用',
                cancelColor: '#000000',
                confirmText: '继续授权',
                confirmColor: '#3CC51F',
                success: (result) => {
                    if (result.confirm) {

                    }
                },
                fail: () => {
                },
                complete: () => {
                }
            });

        }
    })
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : `0${n}`
}
const getUserInfo = function () {
    return getApp().userinfo;
}
const spost = function (url, data, success, fail, withoutOau) {
    post(url, data, success, fail, withoutOau,true)
}
const rpost = function (url, data, success, fail, withoutOau) {
    post(url, data, success, fail, withoutOau,false)
}
const post = function (url, data, success, fail, withoutOau, useSecurityMode) {
    // let withoutOau = true;
    if (!withoutOau) {
        withoutOau = false;
    } else {
        withoutOau = true;
    }
    let requestParam = {
        url: url,
        method: 'POST',
        withoutOau: withoutOau,
        dataType: "json",
        data: data,
        securityMode: useSecurityMode ? "yes":"no",
        success(res) {
            if (res.statusCode == 200) {
                if (success) success(res);
            } else if (res.statusCode == 401) {
                wx.showModal({
                    title: '提示',
                    content: '登录状态已过期，请重新登录',


                    confirmText: '确定',
                    confirmColor: '#3CC51F',
                    success: (result) => {
                        wx.reLaunch({
                            url: '/pages/index/index',
                            success: (result) => {

                            },
                            fail: () => {
                            },
                            complete: () => {
                            }
                        });
                    },
                    fail: () => {
                    },
                    complete: () => {
                    }
                });
            } else {

                if (fail) fail(res);
            }
        },
        fail: function (err) {
            wx.su.toast("数据获取失败，请检查网络后重试");
            if (fail) fail(err);
        }
    }
    wx.su.request(requestParam);
}
const sget = function (url, success, fail, withoutOau) {
    rget(url, success, fail, withoutOau,true)
}
const rget = function (url, success, fail, withoutOau,useSecurityMode) {
    if (!withoutOau) {
        withoutOau = false;
    } else {
        withoutOau = true;
    }

    wx.su.request({
        url: url,
        method: 'GET',
        withoutOau: withoutOau,
        dataType: "json",
        securityMode: useSecurityMode ? "yes":"no",
        success(res) {
            if (res.statusCode == 200) {
                if (success) success(res);
            } else if (res.statusCode == 401) {
                wx.showModal({
                    title: '提示',
                    content: '登录状态已过期，请重新登录',


                    confirmText: '确定',
                    confirmColor: '#3CC51F',
                    success: (result) => {
                        wx.reLaunch({
                            url: '/pages/index/index',
                            success: (result) => {

                            },
                            fail: () => {
                            },
                            complete: () => {
                            }
                        });
                    },
                    fail: () => {
                    },
                    complete: () => {
                    }
                });
            } else {
                wx.su.toast("数据获取失败，请检查网络后重试");
            }
        },
        fail: function () {
            wx.su.toast("数据获取失败，请检查网络后重试");
            if (fail) fail();
        }
    })
}
const contain = function (arr, el) {
    for (let index in arr) {
        if (el == arr[index]) {
            return true;
        }
    }
    return false;
}
module.exports = {
    formatTime,
    getUserProfile,
    // getCarbonInfo,
    // refreshCarbonInfo,
    getUserInfo,
    rget: rget,
    sget:sget,
    rpost: rpost,
    spost:spost,
    contain: contain
}
